package com.neusoft.bizcore.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.dto.OAuth2ConfigDTO;
import com.neusoft.bizcore.auth.service.OAuth2Service;
import com.neusoft.bizcore.web.dto.result.ResultDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("OAuth2对接管理")
@RestController
@RequestMapping(value = "/api/w/auth/oauth2")
public class OAuth2Controller {

    @Autowired
    private OAuth2Service oAuth2Service;

    @ApiOperation("获取OAuth2配置信息")
    @GetMapping
    public ResultDTO<?> getConfig() {
        final OAuth2ConfigDTO dto = this.oAuth2Service.getConfig();
        if (null == dto) {
            return ResultDTO.success();
        }
        return ResultDTO.success(dto);
    }

    @ApiOperation("创建OAuth2配置信息")
    @PostMapping
    public ResultDTO<OAuth2ConfigDTO> create(@RequestBody final OAuth2ConfigDTO dto) {
        final OAuth2ConfigDTO result = this.oAuth2Service.setConfig(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改OAuth2配置信息")
    @PutMapping("/{id}")
    public ResultDTO<OAuth2ConfigDTO> update(@PathVariable final Long id,
            @RequestBody final OAuth2ConfigDTO dto) {
        dto.setId(id);
        final OAuth2ConfigDTO result = this.oAuth2Service.setConfig(dto);
        return ResultDTO.success(result);
    }

    @GetMapping("/{code}")
    public ResultDTO<UserBean> loadByCode(@PathVariable final String code) {
        final UserBean userBean = this.oAuth2Service.loadUserByOAuth2Code(code);
        return ResultDTO.success(userBean);
    }

}
